import 'model_base.dart';

class NoticeModel extends ModelBase {
  final String title;
  final String body;
  final List<String> fileUrls;

  // 构造函数，通过 Map 初始化
  NoticeModel({
    required int id,
    required DateTime createdAt,
    this.title = "",
    this.body = "",
    this.fileUrls = const [],
  }) : super(id: id, createdAt: createdAt);

  // 工厂构造函数，通过 JSON Map 创建 Notice 实例
  factory NoticeModel.fromJson(Map<String, dynamic> map) {
    return NoticeModel(
      id: map["id"] ?? -1,
      createdAt: DateTime.fromMillisecondsSinceEpoch(map["createdAt"] ?? 0),
      title: map["title"] ?? "",
      body: map["body"] ?? "",
      fileUrls: map.containsKey("fileUrls") && map["fileUrls"] is List
          ? List<String>.from(map["fileUrls"])
          : [],
    );
  }

  // 将 Notice 实例转换为 JSON Map
  Map<String, dynamic> toJson() {
    return {
      "id": id,
      "createdAt": createdAt.millisecondsSinceEpoch,
      "title": title,
      "body": body,
      "fileUrls": fileUrls,
    };
  }

  // 静态方法，将动态数据转换为 Notice 列表
  static List<NoticeModel> toList(dynamic res) {
    var data = List<Map<String, dynamic>>.from(res);
    return data.map((map) => NoticeModel.fromJson(map)).toList();
  }
}
